# encoding:utf-8
import requests
from jenkins import Jenkins
from utils.time_tool.time_control import Getdate
from config.all_paths import allure_report_prometheusData, config_variable_path
from utils.file_handling.txt_handling import read_big_txt
from utils.others_tool.other_tool import get_opt
import sys


jenkins_url = "http://106.75.225.58:8080/"
server = Jenkins(jenkins_url, username='admin', password='9d0c48a873cc473db6edad78b819a4fa')
job_name = "job/trade-auto"
job_url = server.get_info(job_name)['url']
job_last_number = server.get_info(job_name)['lastBuild']['number']
report_url = job_url + str(job_last_number) + '/allure'

# 正式环境发布地址
# webhook_pro = r"https://oapi.dingtalk.com/robot/send?access_token=db1e41ef3bdfdbf38b10a7b70aab58edc5454d6a49d09ae653d644dc0ef23baf"
webhook_pro = r"https://oapi.dingtalk.com/robot/send?access_token=b593d4ddf02e820da6136a78e2386a1f0c7c82b933d0e86b398cf824a290edb1"

# 测试发布环境的地址
webhook_test = 'https://oapi.dingtalk.com/robot/send?access_token=95e872da7b500a7c1a692559f6aafdbeda1ba6e0ecf5457a19151d06fa3a184f'

# 技术部内部推送
webhook_it = r'https://oapi.dingtalk.com/robot/send?access_token=3121a21d799dadaa66c03026f6e8efee86c533bbc46d304382ce6b36f4489881'



gmv = eval(read_big_txt(config_variable_path))


def push_message_gmv(webhook=webhook_pro):
    content = {}
    print(allure_report_prometheusData)
    file_path = allure_report_prometheusData
    f = open(file_path)
    for line in f.readlines():
        launch_name = line.strip('\n').split(' ')[0]
        num = line.strip('\n').split(' ')[1]
        content.update({launch_name: num})
    f.close()
    print(content)
    passed_num = content['launch_status_passed']  # 通过数量
    failed_num = content['launch_status_failed']  # 失败数量
    broken_num = content['launch_status_broken']  # 阻塞数量
    skipped_num = content['launch_status_skipped']  # 跳过数量
    case_num = content['launch_retries_run']  # 总数量


    if failed_num == '0' and broken_num == '0':

        content = {
            "msgtype": "markdown",
            "markdown": {
                "title": "自动化监测结果",
                "text": f'## {Getdate().getCurrentday()}' + ' ' + f'{Getdate().getWeekdayToday()}'
                        + "\n### 数据统计监测："
                        + "\n##### 彩虹GMV统计服务：" + '正常'
                        + "\n##### 清宜康GMV统计服务：" + '正常'
                        + "\n##### 日快照统计：" + '正常'
                        + f"\n## {Getdate().getYesterday()} 数据统计 "
                        ########预成康
                        + "\n### 预橙康统计:"
                        + "\n##### 总销售额: " + f"{round(gmv['预橙康-医鹿']['原付金额']+gmv['预橙康-丁香医生']['原付金额']+gmv['预橙康-天猫']['原付金额'], 2)}"
                        + "\n##### 总订单量: " + f"{gmv['预橙康-天猫']['订单量']+gmv['预橙康-丁香医生']['订单量']+gmv['预橙康-医鹿']['订单量']}"

                        + "\n### 预橙康各渠道统计:"
                        + "\n##### 预橙康-医鹿总销售额: " + f"{gmv['预橙康-医鹿']['原付金额']}"
                        + "\n##### 预橙康-医鹿总订单量: " + f"{gmv['预橙康-医鹿']['订单量']}"

                        + "\n##### 预橙康-丁香医生总销售额: " + f"{gmv['预橙康-丁香医生']['原付金额']}"
                        + "\n##### 预橙康-丁香医生总订单量: " + f"{gmv['预橙康-丁香医生']['订单量']}"

                        + "\n##### 预橙康-天猫总销售额: " + f"{gmv['预橙康-天猫']['原付金额']}"
                        + "\n##### 预橙康-天猫总订单量: " + f"{gmv['预橙康-天猫']['订单量']}"

                        ################
                        + "\n### 彩虹统计:"
                        + "\n##### 总销售额: " + f"{round(gmv['彩虹全订单统计']['原付总金额']-gmv['预橙康-医鹿']['原付金额']-gmv['预橙康-丁香医生']['原付金额']-gmv['预橙康-天猫']['原付金额'],2)}"
                        + "\n##### 总订单量: " + f"{gmv['彩虹全订单统计']['总订单量']-gmv['预橙康-天猫']['订单量']-gmv['预橙康-丁香医生']['订单量']-gmv['预橙康-医鹿']['订单量']}"

                        + "\n### 彩虹自营统计:"

                        + "\n##### 自营所有渠道 总销售额: " + f"{round(gmv['彩虹APP']['原付金额']+gmv['彩虹小程序']['原付金额']+gmv['彩虹小程序引流集合']['原付金额']+gmv['彩虹其他']['原付金额'],2)}"
                        + "\n##### 自营所有渠道 总订单量: " + f"{gmv['彩虹APP']['订单量']+gmv['彩虹小程序']['订单量']+gmv['彩虹小程序引流集合']['订单量']+gmv['彩虹其他']['订单量']}"

                        + "\n##### APP商城 总销售额: " + f"{gmv['彩虹APP']['原付金额']}"
                        + "\n##### APP商城 总订单量: " + f"{gmv['彩虹APP']['订单量']}"
                        + "\n##### 微信小程序 总销售额: " + f"{gmv['彩虹小程序']['原付金额']}"
                        + "\n##### 微信小程序 总订单量: " + f"{gmv['彩虹小程序']['订单量']}"
                        + "\n##### 小程序引流集合 总销售额: " + f"{gmv['彩虹小程序引流集合']['原付金额']}"
                        + "\n##### 小程序引流集合 总订单量: " + f"{gmv['彩虹小程序引流集合']['订单量']}"
                        + "\n##### 其他（优选/线下买单） 总销售额: " + f"{gmv['彩虹其他']['原付金额']}"
                        + "\n##### 其他（优选/线下买单） 总订单量: " + f"{gmv['彩虹其他']['订单量']}"

                        + "\n### 清宜康统计: "
                        + "\n##### 总销售额: " + f"{gmv['清宜康全订单统计']['原付总金额']}"
                        + "\n##### 总订单量: " + f"{gmv['清宜康全订单统计']['总订单量']}"

                        + "\n### 清宜康自营统计: "
                        + "\n##### 微信小程序 总销售额: " + f"{gmv['清宜康小程序']['原付金额']}"
                        + "\n##### 微信小程序 总订单量: " + f"{gmv['清宜康小程序']['订单量']}"
                        + "\n##### 无码科技引流 总销售额: " + f"{gmv['清宜康无码科技']['原付金额']}"
                        + "\n##### 无码科技引流 总订单量: " + f"{gmv['清宜康无码科技']['订单量']}"

                        + "\n### 日核销统计: "
                        + "\n##### 总计核销（套）: " + f"{gmv['日核销统计']['总计核销']}"
                        + "\n##### 彩虹(含预橙康)核销（套）: " + f"{gmv['日核销统计']['彩虹核销']}"
                        + "\n##### 清宜康核销（套）: " + f"{gmv['日核销统计']['清宜康核销']}"
                        + "\n##### 9价 已核销(安排医院)（套）: " + f"{gmv['日核销统计']['9价已核销']}"
                        + "\n##### 4价 已核销(安排医院)（套）: " + f"{gmv['日核销统计']['4价已核销']}"
                        + "\n##### 2价 已核销(安排医院)（套）: " + f"{gmv['日核销统计']['2价已核销']}"
                        + "\n##### 其他品类 已核销(安排医院)（套）: " + f"{gmv['日核销统计']['其他品类已核销']}"
                        + "\n##### 当日改医院套数: " + f"{gmv['日核销统计']['当日改医院套数']}"

            }}
        requests.post(url=webhook, json=content, verify=False)
    else:
        content = {
            "msgtype": "text",
            "text": {
                "content": "自动化执行结果：\n日期：" + f'{Getdate().getCurrentday()}' + ' ' + f'{Getdate.getWeekdayToday()}'
                           + "\ngmv数据统计监测：" + '统计数据异常,请检查具体的测试报告！！！'
                           + f"\n{report_url}"
            },
            "at": {
                "atMobiles": [
                    "18017870857",
                ],
                "isAtAll": False
            }
        }
        requests.post(url=webhook_test, json=content, verify=False)

if __name__ == '__main__':
    # push_message_gmv()

    t_opt = get_opt(sys.argv[1:])
    type = t_opt.get('type')
    if type == 'pro':
        push_message_gmv()
        push_message_gmv(webhook=webhook_it)
    if type == 'test':
        push_message_gmv(webhook=webhook_test)

    # python main.py --env=pro --mark=gmv
    # python send_dingding_gmv.py --type=test
